#!/usr/bin/env python

import sys
sys.path.insert(0,'../../')

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meiduo_mall.settings.dev')

# 让Django进行一次初始化
import django
django.setup()

from apps.goods.models import SKU
from apps.goods.goods_utils import get_breadcrumb,get_categories,get_goods_specs
from django.template import loader
from django.conf import settings

def action_static_detail_html(sku):
    breadcrumb = get_breadcrumb(sku.category)
    categories = get_categories()
    goods_specs = get_goods_specs(sku)

    # 构造上下文字典
    context = {
        'sku': sku,
        'categories': categories,
        'breadcrumb': breadcrumb,
        'specs': goods_specs
    }

    template = loader.get_template('detail.html')
    html_str = template.render(context)

    file_path = os.path.join(os.path.dirname(os.path.dirname(settings.BASE_DIR)), 'front_end_pc/goods/'+str(sku.id)+'.html')
    with open(file_path,'w') as f:
        f.write(html_str)

if __name__ == '__main__':
    skus = SKU.objects.all()
    for sku in skus:
        print(sku.id)
        action_static_detail_html(sku)

